superstrict
Include "tgrid.bmx"
include "../common/tormath.bmx" 'distance2d

Global xres# = 640
Global yres# = 480
apptitle = "gridmap user-code test "
graphics xres,yres,0,0,2

local mygrid:TGrid = TGrid.create(0,0,xres,yres,10,10)

local displayObjects:TList = new TList
local closestGO:TGameObject = null

flushkeys()

Local tim:Int, oldtim:Int
local fps:int = 0
local fpsTimer:int = 0
local numframes:int = 0

while not keyhit(KEY_ESCAPE)
	tim = millisecs()
	if tim > fpsTimer
		fps = numframes
		numframes = 0
		fpsTimer = tim + 1000
	endif
	
	numframes:+1
	
	cls()
	
	mygrid.initMap()
	mygrid.drawGrid()
	mygrid.drawConnections()
	
	local createGO:int = false
	local createSiz# = 10
	if keyhit(KEY_1)
		createSiz = 10
		createGO = true
	endif
	
	if keyhit(KEY_2)
		createSiz = 20
		createGO = true
	endif
	
	if keyhit(KEY_3)
		createSiz = 30
		createGO = true
	endif
	
	if createGO
		local go:TGameObject = new TGameObject
		go.siz = createSiz
		go.x = mousex()
		go.y = mousey()
		displayObjects.addLast(go)
		
		mygrid.insertBox(go)
	endif
	
	for local go:TGameObject = eachin displayObjects
		go.draw()
	next

	if mousedown(1) and closestGo = null
		'Find closest obj to mouse
		local mind# = 1e6
		for local go:TGameObject = eachin displayObjects
			local d# = distance2d( go.x, go.y, mousex(), mousey() )
			if d < mind 
				mind = d
				closestGO = go
			endif
		next
	
	else if mousedown(1) and closestGo <> null
			closestGO.isSelected = true
			closestGO.x = mousex()
			closestGO.y = mousey()
			
			setcolor 255,255,255
			Local npcList:TList = mygrid.getIntersectionsWith( closestGo )
			for local npc:TGameObject = eachin npcList
				drawtext "blah blah", npc.x, npc.y
			next
	endif
	
	if not mousedown(1) and closestGo <> null
		closestGo.isSelected = false
		closestGo = null
	endif
	
	'Draw lines between all collision pairs
	if(1)
		local pairs:TList = mygrid.getIntersecting()
		
		setcolor 255,255,255
		setlinewidth 3
		for local p:TCollisionPair = eachin pairs
			local first:TGameObject = TGameObject(p.a)
			local second:TGameObject = TGameObject(p.b)
			drawline( first.xmin+first.getWidth()*.5, first.ymin+first.getHeight()*.5,..
						 second.xmin+second.getWidth()*.5, second.ymin+second.getHeight()*.5 )
		next
		setlinewidth 1
	endif
	
	setalpha 1
	setcolor 255,255,255
	drawtext "go's "+ displayObjects.count(), 15,15
	'drawtext "col pairs this frame: "+ pairs.count(), 15,15*2
	drawtext "col pairs pooled: "+ mygrid.collObjPool.dimensions()[0], 15,15*3
	drawtext "click an object and drag it about to se getIntersectionsWith(obj) in action", 15,15*4
	drawtext "fps:"+fps, 15, graphicsheight()-30
	flip()
wend


type TGameObject extends TBoundingBox 'implements collideable
	field x#,y#
	field siz#
	'field broadphaseGeo:TBoundingBox
	field isSelected:int = false
	
	method new()
		siz = 25
		'broadphaseGeo = new TBoundingBox
		self.setBounds( x-siz/2, y-siz/2, x+siz/2, y+siz/2 )
	end method
	
	method draw()
		setalpha .25
		setblend(LIGHTBLEND)
		if isSelected 
			setcolor 255,0,0
		else
			setcolor 255,255,255
		endif
		
		drawoval( x-siz/2, y-siz/2, siz, siz )
		
		setalpha 1
		setblend(MASKBLEND)
		setcolor 0,0,255
		'broadphaseGeo.set( x-siz/2, y-siz/2, x+siz/2, y+siz/2 )
		self.setBounds( x-siz/2, y-siz/2, x+siz/2, y+siz/2 )
		'drawrectopen( broadphaseGeo.xmin, broadphaseGeo.ymin, broadphaseGeo.xmax, broadphaseGeo.ymax )
		drawrectopen( xmin, ymin, xmax, ymax )

		
		
	end method
end type


